package com.canaan.business.service.inOutStock.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.canaan.business.common.enums.inOutStock.ErpBillTypeEnum;
import com.canaan.business.domain.vo.*;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.inOutStock.SplitBillDetailMapper;
import com.canaan.business.domain.inOutStock.SplitBillDetail;
import com.canaan.business.service.inOutStock.ISplitBillDetailService;
import com.canaan.common.annotation.TableName;
/**
 * 分捡单明细Service业务层处理
 *
 * @author canaan
 * @date 2025-03-29
 */
@Service
public class SplitBillDetailServiceImpl implements ISplitBillDetailService {
    @Resource
    private SplitBillDetailMapper splitBillDetailMapper;

    /**
     * 查询分捡单明细
     *
     * @param id 分捡单明细主键
     * @return 分捡单明细
     */
    @Override
    public SplitBillDetail selectSplitBillDetailById(Long id) {
        return splitBillDetailMapper.selectSplitBillDetailById(id);
    }

    @Override
    public List<SplitBillDetailResultVo> selectSplitBillDetailListPageData(SplitBillDetail splitBillDetail) {
        return splitBillDetailMapper.selectSplitBillDetailListPageData(splitBillDetail);
    }
    /**
     * 查询分拣单数据
     */
    @Override
    public List<TraySplitProductVo> selectSplitBillDetailListByOutStockQueryVo(OutStockQueryVo outStockQueryVo){
        return splitBillDetailMapper.selectSplitBillDetailListByOutStockQueryVo(outStockQueryVo);
    }

    /**
     * 查询分捡单明细列表
     *
     * @param splitBillDetail 分捡单明细
     * @return 分捡单明细
     */
    @Override
    public List<SplitBillDetail> selectSplitBillDetailList(SplitBillDetail splitBillDetail) {
        return splitBillDetailMapper.selectSplitBillDetailList(splitBillDetail);
    }

    /**
     * 新增分捡单明细
     *
     * @param splitBillDetail 分捡单明细
     * @return 结果
     */
    @Override
    public int insertSplitBillDetail(SplitBillDetail splitBillDetail) {
        if(StringUtils.isBlank(splitBillDetail.getNo())){
            splitBillDetail.setNo(TableNoUtil.getNo(SplitBillDetail.class));
        }
        splitBillDetail.setCreateBy(SecurityUtils.getUsername());
        splitBillDetail.setCreateTime(DateUtils.getNowDate());
        return splitBillDetailMapper.insertSplitBillDetail(splitBillDetail);
    }

    /**
     * 批量新增分捡单明细
     *
     * @param splitBillDetailList 分捡单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertSplitBillDetail(List<SplitBillDetail> splitBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (SplitBillDetail splitBillDetail : splitBillDetailList){
            if(StringUtils.isBlank(splitBillDetail.getNo())){
                splitBillDetail.setNo(TableNoUtil.getNo(SplitBillDetail.class));
            }
            splitBillDetail.setCreateBy(username);
            splitBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<splitBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > splitBillDetailList.size()) {
                endIndex = splitBillDetailList.size();
            }
            insertNum = insertNum + splitBillDetailMapper.batchInsertSplitBillDetail(splitBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改分捡单明细
     *
     * @param splitBillDetail 分捡单明细
     * @return 结果
     */
    @Override
    public int updateSplitBillDetail(SplitBillDetail splitBillDetail) {
        splitBillDetail.setUpdateBy(SecurityUtils.getUsername());
        splitBillDetail.setUpdateTime(DateUtils.getNowDate());
        return splitBillDetailMapper.updateSplitBillDetail(splitBillDetail);
    }

    /**
     * 批量修改分捡单明细
     *
     * @param splitBillDetailList 分捡单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateSplitBillDetail(List<SplitBillDetail> splitBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (SplitBillDetail splitBillDetail : splitBillDetailList){
            splitBillDetail.setUpdateBy(username);
            splitBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<splitBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > splitBillDetailList.size()) {
                endIndex = splitBillDetailList.size();
            }
            updateNum = updateNum + splitBillDetailMapper.batchUpdateSplitBillDetail(splitBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除分捡单明细
     *
     * @param ids 需要删除的分捡单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteSplitBillDetailByIds(Long[] ids) {
        return splitBillDetailMapper.deleteSplitBillDetailByIds(ids);
    }

    /**
     * 删除分捡单明细信息
     *
     * @param id 分捡单明细主键
     * @return 结果
     */
    @Override
    public int deleteSplitBillDetailById(Long id) {
        return splitBillDetailMapper.deleteSplitBillDetailById(id);
    }

    /**
     * 查询分捡单明细
     *
     * @param no 编号
     * @return 分捡单明细
     */
    @Override
    public SplitBillDetail selectSplitBillDetailByNo(String no) {
        return splitBillDetailMapper.selectSplitBillDetailByNo(no);
    }

    /**
     * 批量删除分捡单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteSplitBillDetailByNos(String[] nos) {
        return splitBillDetailMapper.deleteSplitBillDetailByNos(nos);
    }

    /**
     * 删除分捡单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteSplitBillDetailByNo(String no) {
        return splitBillDetailMapper.deleteSplitBillDetailByNo(no);
    }


    @Override
    public List<TraySplitBillDetailVo> getSplitBillDetailsByTray(String trayNo, Integer billType) {
        List<TraySplitBillDetailVo> detailVoList = new ArrayList<>();
        List<SplitBillDetailInfoVo> list;
        if (ErpBillTypeEnum.PURCHASE_RETURN.getCode() == billType) {
            list = splitBillDetailMapper.getPurchaseReturnSplitBillDetailsByTray(trayNo);
        } else if (ErpBillTypeEnum.ADJUST_OUT.getCode() == billType) {
            list = splitBillDetailMapper.getAjustOutSplitBillDetailsByTray(trayNo);
        } else if (ErpBillTypeEnum.MODULATION_OUT.getCode() == billType) {
            list = splitBillDetailMapper.getModulationOutSplitBillDetailsByTray(trayNo);
        } else {
            list = new ArrayList<>();
        }
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        Map<String, List<SplitBillDetailInfoVo>> maps = list.stream().collect(Collectors.groupingBy(SplitBillDetailInfoVo::getRemark));
        for (Map.Entry<String, List<SplitBillDetailInfoVo>> entry : maps.entrySet()) {
            TraySplitBillDetailVo detailVo = new TraySplitBillDetailVo();
            detailVo.setBillNo(entry.getKey());
            detailVo.setDetai1s(entry.getValue());
            detailVoList.add(detailVo);
        }
        return detailVoList;
    }
}
